<?php
namespace app\manage\controller;
use app\manage\controller\Base;
use think\facade\Db;
use support\View;
use support\Request;
use app\model\MessageModel;


class MessageController extends Base
{
    public function index(Request $request)
    {
    	$link=new MessageModel();
		$key=$request->get('key','');
        View::assign('key',$key);
        $link=$link->whereor('title','like','%'.$key.'%')
                        ->order('id desc')
                        ->paginate(['list_rows'=>10,'query'=>request()->all()]);
        View::assign('comment',$link);
        View::assign('count1',$link->total());
       	return view('message/index');
    }
	public function ajax(Request $request)
    {
    	$data=$request->post();
		$message=new MessageModel();
		if($data['type']=='member_del'){
			$id=$data['id'];
			$info=$message->destroy($id);
			$liu=$message->where('isopen','0')->count();
			if($info){
				return json([1,$liu]);
			}else{
				return 0;
			}
		}
		return 0;
    }
	public function edit(Request $request)
    {
    	$message=new MessageModel();
    	if(request()->method()=='POST'){
    		$data=$request->post();
			$res=$message->update($data,['id' => $data['id']]);
			$liu=$message->where('isopen','0')->count();
			if($res){
				if($liu!=0){
					echo '<script>parent.parent.document.getElementById("lizhili_liu").innerHTML="留',$liu,'"</script>';
				}else{
					echo '<script>parent.parent.document.getElementById("lizhili_liu").innerHTML="留"</script>';
				}
				return view_s('修改成功',url('message/index',['st'=>1]));
			}else{
				return view_e('修改失败了');
			}
    	}
		$id=$request->get('id',0);
		$data=$message->find($id);
        View::assign('data',$data);
       return view('message/edit');
    }
}
